package com.zzhou.common.library.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * <desc>
 * Created by The Moss on 2018/6/30.
 */

public class Utils {

    public static int randomInt() {
        Random sRandom = new Random();
        return sRandom.nextInt();
    }

    public static long randomLong() {
        Random sRandom = new Random();
        return sRandom.nextLong();
    }

    /**
     * 判断 用户是否安装微信客户端
     */
    public static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断 用户是否安装QQ客户端
     */
    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equalsIgnoreCase("com.tencent.qqlite") || pn.equalsIgnoreCase("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断 用户是否安装QQZone客户端
     */
    public static boolean isQQZoneClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                LogUtils.e("pn = " + pn);
                if (pn.equalsIgnoreCase("com.qzone")) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 精彩资讯接口的token
     */
    public static String getNewsToken() {
        String extend = "aheducloud";
        long time = System.currentTimeMillis() / 1000;
        String token = "";
        token = CipherUtils.md5(extend + time).toLowerCase() + "|" + time;
        return token;
    }

    //获取app版本号
    public static String getVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(),
                    0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "和教育";
        }
    }

    /**
     * 判断是否安装某客户端
     */
    public static boolean checkApkExist(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName))
            return false;
        try {
            ApplicationInfo info = context.getPackageManager()
                    .getApplicationInfo(packageName,
                            PackageManager.GET_UNINSTALLED_PACKAGES);
            if (info == null) {
                return false;
            } else {
                return true;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    //运行第三方app
    public static void runAppByParam(Context context, String packageName,
                                     Map<String, Object> params) {
        PackageInfo pi;
        try {
            pi = context.getPackageManager().getPackageInfo(packageName, 0);
            Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
            resolveIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            resolveIntent.setPackage(pi.packageName);
            PackageManager pManager = context.getPackageManager();
            List<ResolveInfo> apps = pManager.queryIntentActivities(resolveIntent, 0);
            ResolveInfo ri = apps.iterator().next();
            if (ri != null) {
                packageName = ri.activityInfo.packageName;
                String className = ri.activityInfo.name;
                Intent intent = new Intent(Intent.ACTION_MAIN);
                ComponentName cn = new ComponentName(packageName, className);
                intent.setComponent(cn);
                if (params != null && params.size() > 0) {
                    Set<Map.Entry<String, Object>> set = params.entrySet();
                    for (Iterator<Map.Entry<String, Object>> it = set
                            .iterator(); it.hasNext(); ) {
                        Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
                                .next();
                        intent.putExtra(entry.getKey(), entry.getValue()
                                .toString());
                    }
                }
                context.startActivity(intent);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void runAppByParam(Context context, String packageName,
                                     String launchUrl, Map<String, Object> params) {
        ComponentName componetName = new ComponentName(packageName, launchUrl);
        try {
            Intent intent = new Intent();
            intent.setComponent(componetName);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (params != null && params.size() > 0) {
                Set<Map.Entry<String, Object>> set = params.entrySet();
                for (Iterator<Map.Entry<String, Object>> it = set.iterator(); it
                        .hasNext(); ) {
                    Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it
                            .next();
                    intent.putExtra(entry.getKey(), entry.getValue().toString());
                }
            }
            context.startActivity(intent);
        } catch (Exception e) {
            ToastShow.show(context, "应用未安装或路径不存在");
        }
    }
}
